# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Nathan Angelacos <nangel@alpinelinux.org>
# Contributor: TBK <alpine@jjtc.eu>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Duncan Bellamy <dunk@denkimushi.com>
# Maintainer: Duncan Bellamy <dunk@denkimushi.com>
pkgname=rspamd
pkgver=3.8.4
pkgrel=0
pkgdesc="Fast, free and open-source spam filtering system"
url="https://rspamd.com/"
arch="all"
license="Apache-2.0 BSD-1-Clause BSD-2-Clause BSD-3-Clause CC0-1.0 LGPL-2.1-or-later
	LGPL-3.0-only MIT Zlib"
pkgusers="rspamd"
pkggroups="rspamd"
depends="icu-data-full"
makedepends="
	cmake
	curl-dev
	elfutils-dev
	fmt-dev
	glib-dev
	icu-dev
	libsodium-dev
	libstemmer-dev
	libunwind-dev
	fasttext-dev
	openssl-dev>3
	pcre2-dev
	perl
	ragel
	samurai
	sqlite-dev
	xxhash-dev
	zlib-dev
	zstd-dev
	"
case "$CARCH" in
	ppc64le|riscv64|s390x|x86|loongarch64) makedepends="$makedepends lua5.4-dev"
		_luajit="OFF" ;;
	*) makedepends="$makedepends luajit-dev"
		_luajit="ON" ;;
esac
install="$pkgname.pre-install $pkgname.post-upgrade"
subpackages="
	$pkgname-dbg
	$pkgname-doc
	$pkgname-client
	$pkgname-libs
	$pkgname-utils::noarch
	$pkgname-openrc
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/rspamd/rspamd/archive/$pkgver.tar.gz
	$pkgname.logrotated
	$pkgname.initd
	$pkgname.confd
	10-control_socket.patch
	20-default-configs.patch
	30-unbundle-snowball.patch
	no-avx-test.patch
	tests.patch
	"
# merged into main package
provides="
	rspamd-controller=$pkgver-r$pkgrel
	rspamd-fuzzy=$pkgver-r$pkgrel
	rspamd-proxy=$pkgver-r$pkgrel
	"
replaces="
	rspamd-controller
	rspamd-fuzzy
	rspamd-proxy
	"

case "$CARCH" in
	x86_64|aarch64|ppc64le)
		makedepends="$makedepends vectorscan-dev"
		_hm="ON" ;;
	*)
		_hm="OFF" ;;
esac

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	case "$CARCH" in
		arm*) local _backward=OFF ;;
		*) local _backward=ON ;;
	esac
	cmake -B build \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCONFDIR=/etc/$pkgname \
		-DRUNDIR=/run/$pkgname \
		-DRSPAMD_USER=$pkgusers \
		-DRSPAMD_GROUP=$pkggroups \
		-DENABLE_FASTTEXT=ON \
		-DSYSTEM_FMT=ON \
		-DENABLE_BACKWARD="$_backward" \
		-DENABLE_HYPERSCAN="$_hm" \
		-DENABLE_LUAJIT="$_luajit" \
		-DENABLE_PCRE2=ON \
		-DENABLE_URL_INCLUDE=ON \
		-DSYSTEM_XXHASH=ON \
		-DSYSTEM_ZSTD=ON \
		-GNinja \
		$CMAKE_CROSSOPTS
	cmake --build build --target all check
}

check() {
	cmake --build build --target run-test
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	cd "$pkgdir"

	install -D usr/share/"$pkgname"/www/README.md \
		usr/share/doc/"$pkgname"/README.md
	rm usr/share/"$pkgname"/www/README.md

	install -dm 750 -o rspamd -g rspamd \
		var/lib/$pkgname
	install -dm 750 -o rspamd -g rspamd \
		var/log/$pkgname
	install -dm 755 -o rspamd -g rspamd \
		etc/$pkgname/local.d/maps.d

	install -Dm 644 "$srcdir"/$pkgname.logrotated etc/logrotate.d/$pkgname
	install -Dm 755 "$srcdir"/$pkgname.initd etc/init.d/$pkgname
	install -Dm 644 "$srcdir"/$pkgname.confd etc/conf.d/$pkgname
	install -d etc/"$pkgname"/local.d etc/"$pkgname"/override.d
}

client() {
	pkgdesc="$pkgdesc (console client)"

	amove usr/bin/rspamc*
}

libs() {
	pkgdesc="$pkgdesc (libraries)"

	amove usr/lib/$pkgname/*.so
}

utils() {
	pkgdesc="$pkgdesc (utilities)"
	depends="perl"

	amove usr/bin/"$pkgname"_stats
}

sha512sums="
dda099dd9a17699d143fc8018b89fbecfa9659eccc26b5fb88bc3f40c47e935993dfc81f1f93bb69880d17af40870e6ea20edbeed7e3f4c12c278f5c12b56a51  rspamd-3.8.4.tar.gz
2efe28575c40d1fba84b189bb872860e744400db80dce2f6330be6c6287fb3f46e6511284729b957488bf40bcb9b0952e26df9934f5f138334bd2766075c45cb  rspamd.logrotated
c136126e3d3953079939b2b0b9b49f5f921af4c3aa0ad59a206632436ff963a69b5980a3b8e4eb14882d8db31d6fd96045a9e1cd6302ad111d76b8e92fab890e  rspamd.initd
a2003ef0c9d64a44480f59302864a2dfedcbe3a0047fcbb655408bc8aae9014b6ad0ddc6b64d4abeeb21bea0f86678afd30589ac8eed83e07ad7f87710e93702  rspamd.confd
4ce3086f0c1ef46c1cf6e0414fc480f6f9a2da2a175acd9b7a059af6a5f59fdf564703e35ba91d646a045b6213c983d97d876cbc142259668351d476791f5ee7  10-control_socket.patch
8801e3af59886847c25c87ca2f231cd9ededf35f376f9920967e72696fb017b1a4312f3a49017b52f65556bfe5c9f74a80405643afa32bb2906c38d39c5e1818  20-default-configs.patch
3bb8dd5525e9370e134922c0f37d08b9287432f2f146476ab2dd635e1ea6ccc77143b5b3f721b959dc5721ef2c00f66656d7c5eab4874f81022d803a50b7620e  30-unbundle-snowball.patch
967f28425105b697f3cdaf38478cd4330ecab814f0296fe6167977156df914a399a685bd72886eaeb262dabab9ccfd061198fffcdfe959dbb988e95a397cf86d  no-avx-test.patch
e8804ce6dd8d6d0cb70db94a6c4b4cb60a05f22e6434f2383a7502b4c6e653154bd527ca5d2a77ae949ca190f7498d4021590607555118e88d1d4199fef5dd48  tests.patch
"
